/**
 * 
 */
package com.taobao.server;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.taobao.api.direct.TaobaoDirectJsonRestClient;
import com.taobao.client.UpperService;
import com.taobao.client.obj.TopItem;
import com.taobao.client.obj.TopRefund;
import com.taobao.client.obj.TopTrade;
import com.taobao.service.SessionService;
import com.taobao.service.TradeService;

/**
 * The server side implementation of the RPC service.
 * 
 * @author hujingfei.pt@taobao.com
 * 
 */
public class UpperServiceImpl implements UpperService {

	private TradeService tradeService;
	private SessionService sessionService;
	public final static String CONSTANT_NUMBER = "300";
	
	
	/**
	 * Default Constructor
	 */
	public UpperServiceImpl(TradeService tradeService, SessionService sessionService){
		System.out.println("UpperServiceImpl::UpperServiceImpl::Succeed!");
		this.tradeService = tradeService;
		this.sessionService = sessionService;
	}
	
	/**
	 * Passing several essential parameters to the back-end.
	 * 
	 * @param url the url used to access the TOP api
	 * @param app the key of the application
	 * @param secrt the password of the application
	 * @param loginAddress the url used to login
	 * @return if initialized successfully, return true, or return false
	 * @throws Exception when some services behind back-end is unavailable, throw exceptions
	 */
	public boolean init(String url, String app, String secrt, String loginAddress) throws Exception {
		return sessionService.init(url, app, secrt, loginAddress);
	}

	/**
	 * Create a trade between the buyer and the seller
	 * 
	 * @param item the item covered in the trade
	 * @param buyer the nickname of the buyer
	 * @param seller the nickname of the seller
	 * @throws Exception when some services behind back-end is unavailable, throw exceptions
	 */
	public void addTrade(List<TopItem> item,String buyer, String seller,String name, String phone, String mobile, String province,
			String city, String area, String division_code, String post_code, String address_detail, boolean isNewAddress, String memo,
			String discount)throws Exception {
		tradeService.addTrade(item, buyer, seller, name, phone, mobile, province, city, area, division_code, post_code, 
				address_detail, isNewAddress, memo, discount);
	}

	/**
	 * Seller sends the item
	 * 
	 * @param tid the id of the trade
	 * @throws Exception Exception when some services behind back-end is unavailable, throw exceptions
	 */
	public void deliverySend(String tid) throws Exception {
		tradeService.deliverySend(tid);
	}

	/**
	 * Get a list of trades as a seller
	 * 
	 * @param nick the nickname of the seller
	 * @return a list of trades
	 * @throws Exception when some services behind back-end is unavailable, throw exceptions
	 */
	public List<TopTrade> tradeSoldGet(String nick) throws Exception {
		return tradeService.tradeSoldGet(getLoginName());
	}

	/**
	 * Get the session of the user
	 * 
	 * @param userName the user name
	 * @param password the user password
	 * @return the session id(key)
	 * @throws Exception when some services behind back-end is unavailable, throw exceptions
	 */
	public String getSession(String userName, String password) throws Exception {
		return sessionService.getSession(userName, password);
	}

	/**
	 * Get the login url
	 * 
	 * @param domain the domain of the callback url.
	 * @param url the callback url
	 */
	public String getLoginURL(String domain, String url) {
		return sessionService.getLoginURL(domain, url);
	}

	/**
	 * Get a list of trades as a buyer
	 * 
	 * @param nick the nickname of the buyer
	 * @return a list of trades
	 * @throws Exception when some services behind back-end is unavailable, throw exceptions
	 */
	public List<TopTrade> tradeBoughtGet(String nick) throws Exception {
		ArrayList<TopTrade> tradeList = new ArrayList<TopTrade>();
		if (tradeService == null) {
			System.out.println("UpperServiceImpl::TradeService is not initialized yet!");
			return tradeList;
		} else {
			return tradeService.tradeBoughtGet(sessionService.getLoginName());
		}
	}

	/**
	 * Return the session id(key)
	 * 
	 * @return if there is a session key in the session object, return it, or return null
	 */
	public String getSession() {
		return sessionService.getSession();
	}

	/**
	 * Decode login name from topParameters and store it and session
	 * 
	 * @param session the session
	 * @param topParameters parameters encoded
	 */
	public String setSession(String session, String topParameters) {
		return sessionService.setSession(session, topParameters);
	}

	/**
	 * Return the login name of the user
	 * 
	 * @return if there is a login name in the session object, return it, or return null
	 */
	public String getLoginName() {
		return sessionService.getLoginName();
	}

	
	public String getUrl(Map<String, String> params) {
		return sessionService.getUrl(params);
	}

	/**
	 * Get the refunds the buyer applied
	 * 
	 * @param nick the nickname of the buyer
	 * @return a list of refunds
	 * @throws Exception when some services behind back-end is unavailable, throw exceptions
	 */
	public List<TopRefund> getBuyerRefunds(String nick) throws Exception {
		return tradeService.getBuyerRefunds(nick);
	}

	/**
	 * Get the refunds the seller received
	 * 
	 * @param seller the nickname of the seller
	 * @return a list of refunds
	 * @throws Exception when some services behind back-end is unavailable, throw exceptions
	 */
	public List<TopRefund> getSellerRefund(String seller) throws Exception {
		return tradeService.getSellerRefund(seller);
	}

	/**
	 * Return the client object
	 * 
	 * @return the client object
	 */
	public TaobaoDirectJsonRestClient getClient() {
		return sessionService.getClient();
	}

	/**
	 * Get the items of the seller
	 * 
	 * @param nick the nickname of the seller
	 * @return a list of items
	 * @throws Exception when some services behind back-end is unavailable, throw exceptions
	 */
	public List<TopItem> itemsGet(String nick) throws Exception {
		return tradeService.itemsGet(nick);
	}
	
	/**
	 * The user logout
	 * 
	 * @param url the callback url
	 * @param domain the domain
	 * @return
	 */
	public String logout(String domain, String url) {
		return sessionService.logout(domain, url);
	}
	
	/**
	 * Get the concrete information of the refund
	 * 
	 * @param refundId the id of the refund.
	 * @return a refund object.
	 * @throws Exception when some services behind back-end is unavailable, throw exceptions
	 */
	public TopRefund getRefundInfo(String refundId) throws Exception {
		return tradeService.getRefundInfo(refundId);
	}
}
